package com.micmac.main.mainmodel.service
{

	/*
	
	Author : Roy Raul.
	
	Component : ConfigService.             
	
	Version History         : 0.1 Date Jul-20-2011
	
	Current Version         : 0.1 Date Jul-20-2011
	
	Build Date              : Jul-20-2011
	
	*/
	
	import com.micmac.main.interfaces.IStartupService;
	import com.micmac.main.mainmodel.business.LoadXMLDelegate;
	import com.micmac.main.mainmodel.helpers.XmlResource;
	import com.micmac.main.mainmodel.interfaces.IStartupModel;
	
	import flash.events.Event;
	
	import mx.rpc.IResponder;
	
	import org.robotlegs.mvcs.Actor;
	
	import spark.components.mediaClasses.VolumeBar;

	public class ConfigService extends Actor implements IStartupService, IResponder
	{
		public static const NAME:String = "configService";
		
		[Inject]
		public var startupMonitorModel:IStartupModel;
		
		private var data:Object;
		
		private var configPath:String = "assets/data/config.xml";
		
		public function register():void
		{
			startupMonitorModel.addResource( this , true );
		}

	
		public function load():void{
			
			// reset the data 
			this.data = new Object();
			// create a worker who will go get some data
			// pass it a reference to this proxy so the delegate knows where to return the data
			var delegate : LoadXMLDelegate = new LoadXMLDelegate(this,  configPath );
			// make the delegate do some work
			delegate.load();
		}
		
		// this is called when the delegate receives a result from the service
		public function result( rpcEvent : Object ) : void{
			// call the helper class for parse the XML data
			XmlResource.parse(data, rpcEvent.result);
			// call the StartupMonitorProxy for notify that the resource is loaded
			startupMonitorModel.resourceComplete( this );
		}
		
		// this is called when the delegate receives a fault from the service
		public function fault( rpcEvent : Object ) : void{
			// send the failed notification
			//dispatch(new SplashViewEvent(SplashViewEvent.LOADING_ERROR,0,true));
			
		}
		
		/**
		 * Get the config value
		 * 
		 * @param key the key to read 
		 * @return String the key value stored in internal object
		 */
		
		public function getValue(key:String):Object{
			return data[key.toLowerCase()];
		}
	}
}